/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bombercloud.client;

import bombercloud.commons.GameAction;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 *
 * @author leoomoreira
 */
public class Player extends Thread {

    private int id;
    private boolean active;
    private Random random;

    public int avatarWidth = -1;
    public int avatarHeight = -1;

    public int dimensionWidth = -1;
    public int dimensionHeight = -1;

    private String serverAddress;
    private int serverPort;

    private final PrintWriter experimentLog;
    private SimpleDateFormat simpleDateFormat;

    public Player(int id, int dimensionWidth, int dimensionHeight, int avatarWidth, int avatarHeight, String serverAddress, int serverPort, final PrintWriter experimentLog, SimpleDateFormat simpleDateFormat) {
        this.id = id;
        this.dimensionWidth = dimensionWidth;
        this.dimensionHeight = dimensionHeight;
        this.avatarWidth = avatarWidth;
        this.avatarHeight = avatarHeight;
        this.serverAddress = serverAddress;
        this.serverPort = serverPort;
        this.experimentLog = experimentLog;
        this.simpleDateFormat = simpleDateFormat;
        random = new Random();
    }

    @Override
    public synchronized void start() {
        active = true;
        super.start();
    }

    @Override
    public void run() {
        Socket serverSocket = null;
        ObjectInputStream serverInputStream = null;
        ObjectOutputStream serverOutputStream = null;
        try {
            serverSocket = new Socket(serverAddress, serverPort);
            serverInputStream = new ObjectInputStream(serverSocket.getInputStream());
            serverOutputStream = new ObjectOutputStream(serverSocket.getOutputStream());
        } catch (IOException ex) {
            serverInputStream = null;
            serverOutputStream = null;
        }
        while (active) {
            int action = random.nextInt(4);
            if (action < 0 || action > 3) {
                continue;
            }
            GameAction gameAction = new GameAction(dimensionWidth, dimensionHeight, avatarWidth, avatarHeight);
            gameAction.setPlayerId(id);
            gameAction.setAction(action);

            long startResponseTime = System.currentTimeMillis();
            boolean processed = false;

            try {
                serverOutputStream.writeObject(gameAction);
                serverOutputStream.flush();
                gameAction = (GameAction) serverInputStream.readObject();
                processed = gameAction.isProcessed();
            } catch (NullPointerException | IOException | ClassNotFoundException ex) {
                processed = false;
            }

            long endResponseTime = System.currentTimeMillis();
            long responseTime = endResponseTime - startResponseTime;
            synchronized (experimentLog) {
                if (active) {
                    experimentLog.println(simpleDateFormat.format(new Date()) + ";" + id + ";" + gameAction.getAction() + ";" + responseTime + ";" + processed);
                }
            }
        }
        try {
            serverSocket.close();
        } catch (IOException ex) {
        }
    }

    public void deactivate() {
        active = false;
    }

}
